﻿// HomeInfoForm.xaml.cs
// Aleksei Vassiljev
// {4DC17B88-5237-4e95-A60D-72988F554D4F}

using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using OracleTools.Core;
using OracleTools.OracleHomeSelector.Resources;

namespace OracleTools.OracleHomeSelector
{
    /// <summary>
    /// Interaction logic for HomeInfoForm.xaml
    /// </summary>
    public partial class HomeInfoForm : Window
    {
        /*public enum Commands : int
        {
            ShowHomeInfo = 1
        }*/
        //public HomeInfoForm.Commands Command { get; set; }
        public OraHome SelectedHome { get; set; }
        private OraMaster m_oramaster;

        /// <summary>
        /// List of tnsnames.ora files
        /// </summary>
        private List<NetFileInfo> m_lstTnsFiles;
        /// <summary>
        /// List of sqlnet.ora files
        /// </summary>
        private List<NetFileInfo> m_lstSqlNetFiles;


        public HomeInfoForm(OraMaster oramaster)
        {
            InitializeComponent();
            this.m_oramaster = oramaster;
            m_lstTnsFiles = new List<NetFileInfo>();
            m_lstSqlNetFiles = new List<NetFileInfo>();

            // Event handlers' declarations

            // Form and tab control
            this.KeyDown += new KeyEventHandler(HomeInfoForm_KeyDown);
            this.Loaded += new RoutedEventHandler(HomeInfoForm_Loaded);
            tabControl.SelectionChanged += new SelectionChangedEventHandler(tabControl_SelectionChanged);

            // Tab "Home Info"
            cmdOpenTns.Click += new RoutedEventHandler((sx, ex) => RunExternal(txtTnsOra.Text));
            cmdOpenSqlNet.Click += new RoutedEventHandler((sx, ex) => RunExternal(txtSqlNet.Text));
            cmdOpenLdap.Click += new RoutedEventHandler((sx, ex) => RunExternal(txtLdapOra.Text));
            cmdOpenPath.Click += new RoutedEventHandler((sx, ex) => RunExternal(txtPath.Text));

            lstHomes.SelectionChanged += new SelectionChangedEventHandler(lstHomes_SelectionChanged);
            
            // Tab "File Sync"
            lstTns.SelectionChanged += new SelectionChangedEventHandler(lstTns_SelectionChanged);
            cmdCopySelectedTns.Click += new RoutedEventHandler(cmdCopySelectedTns_Click);
            lstSql.SelectionChanged += new SelectionChangedEventHandler(lstSql_SelectionChanged);
            cmdCopySelectedSql.Click += new RoutedEventHandler(cmdCopySelectedSql_Click);
        }

        

        #region Private Methods

        private NetFileInfo GetNetFileInfo(OraHome home, string fileName)
        {
            FileInfo fi = new FileInfo(fileName);
            DateTime dt = fi.LastWriteTime;
            long nSize = fi.Length;
            return new NetFileInfo(home.Name, home.Path, fileName, dt, nSize);
        }

        private void RefreshTnsFiles()
        {
            m_lstTnsFiles.Clear();
            m_oramaster.Homes.ForEach(x => m_lstTnsFiles.Add(GetNetFileInfo(x, x.TnsOraFilePath)));
            lstTns.Items.Refresh();
        }
        private void RefreshSqlNetFiles()
        {
            m_lstSqlNetFiles.Clear();
            m_oramaster.Homes.ForEach(x => m_lstSqlNetFiles.Add(GetNetFileInfo(x, x.SqlNetOraFilePath)));
            lstSql.Items.Refresh();
        }

        private void ShowHomeInfo(OraHome home)
        {
            txtName.Text = home.Name;
            txtVersion.Text = home.Version.ToString();
            txtArchitecture.Text = home.X64 ? "x64" : "x86";
            txtBundleName.Text = home.BundleName;
            txtGroupName.Text = home.GroupName;
            txtNLS.Text = home.NlsLang;
            txtPath.Text = home.Path;
            txtTnsOra.Text = home.TnsOraFilePath;
            txtSqlNet.Text = home.SqlNetOraFilePath;
            txtLdapOra.Text = home.LdapOraFilePath;
            txtOLEDB.Text = home.OLEDB;
            txtOO4O.Text = home.OO4O;

            cmdOpenTns.IsEnabled = !string.IsNullOrWhiteSpace(home.TnsOraFilePath);
            cmdOpenSqlNet.IsEnabled = !string.IsNullOrWhiteSpace(home.SqlNetOraFilePath);
            cmdOpenLdap.IsEnabled = !string.IsNullOrWhiteSpace(home.LdapOraFilePath);

            lstHomes.SelectedItem = home;

        }

        private void RunExternal(string cmd)
        {
            System.Diagnostics.Process.Start(cmd);
        }

        private void CopySelectedFile(NetFileInfo fileSelected, List<NetFileInfo> files, Action refreshAction)
        {

            if (MessageBox.Show(string.Format(ResText.FILE_COPY_MESSAGE, fileSelected.FileName),
                ResText.FILES_OVERWRITE_CONFIRM,
                MessageBoxButton.YesNo,
                MessageBoxImage.Question) == MessageBoxResult.Yes)
            {
                foreach (NetFileInfo fi in files)
                {
                    if (fi.FileName != fileSelected.FileName)
                        File.Copy(fileSelected.FileName, fi.FileName, true);
                }
                refreshAction.Invoke();
            }
        }
        #endregion Private Methods


        #region Event Handlers

        #region Form and tab control
        void HomeInfoForm_Loaded(object sender, RoutedEventArgs e)
        {
            lstHomes.ItemsSource = m_oramaster.Homes;
            lstTns.ItemsSource = m_lstTnsFiles;
            lstSql.ItemsSource = m_lstSqlNetFiles;

            // Commands
            //if (this.Command == Commands.ShowHomeInfo)
            //{
                tabHomeInfo.IsSelected = true;
                if (SelectedHome != null)
                    ShowHomeInfo(SelectedHome);
                else
                    ShowHomeInfo(m_oramaster.CurrentHome);
            //}

        }
        void HomeInfoForm_KeyDown(object sender, KeyEventArgs e)
        {
            // Close form on Esc
            if (e.Key == Key.Escape)
                this.Close();
        }

        // Selected tab changed
        void tabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                TabItem tabNew = tabControl.SelectedItem as TabItem;
                if (tabNew.Name == "tabHomeInfo")
                {
                }
                else if (tabNew.Name == "tabFileSync")
                {
                    if (m_lstTnsFiles.Count == 0)
                        RefreshTnsFiles();
                    if (m_lstSqlNetFiles.Count == 0)
                        RefreshSqlNetFiles();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        #endregion Form and tab control

        #region Tab "Home Info"
        void lstHomes_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.SelectedHome = e.AddedItems[0] as OraHome;
            ShowHomeInfo(this.SelectedHome);
        }

        
        #endregion Tab "Home Info"

        #region Tab "File Sync"
        void cmdCopySelectedSql_Click(object sender, RoutedEventArgs e)
        {
            NetFileInfo selFile = (lstSql.SelectedItem as NetFileInfo);
            CopySelectedFile(selFile, m_lstSqlNetFiles, () => RefreshSqlNetFiles());
        }

        void cmdCopySelectedTns_Click(object sender, RoutedEventArgs e)
        {
            NetFileInfo selFile = (lstTns.SelectedItem as NetFileInfo);
            CopySelectedFile(selFile, m_lstTnsFiles, () => RefreshTnsFiles());
        }

        void lstSql_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            cmdCopySelectedSql.IsEnabled = true;
        }

        void lstTns_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            cmdCopySelectedTns.IsEnabled = true;
        }


        #endregion Tab "File Sync"





        #endregion Event Handlers
    }
}
